package com.daykw.xiaohongshu.task;

import java.util.List;
import java.util.concurrent.ExecutorService;

import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;
import org.quartz.DisallowConcurrentExecution;
import org.springframework.stereotype.Service;

import com.daykw.xiaohongshu.model.UpdateAid;
import com.daykw.xiaohongshu.task.thread.RestCommentThread;
import com.daykw.xiaohongshu.util.Const;
import com.daykw.xiaohongshu.util.SpringContextHolder;
import com.daykw.xiaohongshu.util.ThreadPools;
import com.daykw.xiaohongshu.util.ZKC;

@DisallowConcurrentExecution
@Service
public class RestCommentJob implements Runnable {

	private static Logger logger = Logger.getLogger(RestCommentJob.class);

	@Override
	public void run() {

		NutDao dao = SpringContextHolder.getBean("dao");

		try {

			String node = Const.getLocalHostIp();

			int index = ZKC.getZKClient().getNodeIndex(node);

			if (index == 0) {
				List<UpdateAid> list = dao.query(UpdateAid.class,
						Cnd.where("comment_status", "=", 0).and("status", "=", 2).limit(1, 10));
				ExecutorService pool = ThreadPools.getThreadPool(this, 10);
				logger.info("节点:" + node + ",当前取摸索引:" + index);
				for (UpdateAid task : list) {
					RestCommentThread thread = new RestCommentThread(task);
					pool.execute(thread);
				}
				logger.info("重置任务增加************" + list.size() + "************");
			} else {
				logger.info("!!!!!!!!!!!!!!!!!不是主节点");
			}

		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

}
